package com.example.my_club2.welcome

import android.content.Context
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils


import com.example.my_club2.R
import com.example.my_club2.databinding.ActivityHomeWelcomeBinding
import com.jczy.cyclone.dialog.MessageNoticeDialog
import com.therouter.TheRouter
import java.util.Timer
import java.util.TimerTask



class HomeWelcomeActivity : AppCompatActivity() {
    var splashAgreement = SPUtils.getInstance().getBoolean("splashAgreement",false)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home_welcome)
        if (!splashAgreement){
            val colorMain = ContextCompat.getColor(this,R.color.red)
            val userAgreement = "《用户协议》"
            val privacyPolicy = "《隐私条款》"

            // 2. 拼接完整协议内容
            val content =
                "宗申骑士俱乐部非常尊重用户个人的信息保护，我们依据最新的监管要求特向您说明如下：\n1.在使用 APP过程中，我们有可能会收集、使用设备标识信息用于推送车辆提醒消息。\n2.我们会一直采取严格的安全技术保护您的个人信息安全。\n3.未经您的同意，我们不会以协议约定之外的形式和渠道获取、共享或使用您的个人信息。\n您可通过阅读完整的${userAgreement}和${privacyPolicy}来了解详细信息。请您充分阅读并理解以上协议，点击同意按钮，即表示您已同意上述协议及以上约定。"

            // 3. 创建富文本构建器
            val spannableStringBuilder = SpannableStringBuilder(content)

            // 4. 为《用户协议》添加点击事件
            val userAgreementStart = content.indexOf(userAgreement)
            val userAgreementEnd = userAgreementStart + userAgreement.length
            spannableStringBuilder.setSpan(object : ClickableSpan() {
                override fun onClick(p0: View) {
                    // 点击后跳转到用户协议网页
                    ToastUtils.showLong("点击了用户协议")
                    TheRouter.build("app/CommonWebActivity").withString("url","http://moto-cloud.qiyumotor.com/mobile/agreement?type=2").withString("text",userAgreement).navigation()
                }
                override fun updateDrawState(ds: TextPaint) {
                    ds.color = colorMain

                    ds.isUnderlineText = false // 不显示下划线（优化视觉）
                }
            }, userAgreementStart, userAgreementEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)

            // 5. 为《隐私条款》添加点击事件（逻辑同上）
            val privacyPolicyStart = content.indexOf(privacyPolicy)
            val privacyPolicyEnd = privacyPolicyStart + privacyPolicy.length
            spannableStringBuilder.setSpan(object : ClickableSpan() {
                override fun onClick(p0: View) {
                    // 点击后跳转到隐私条款网页
                    ToastUtils.showLong("点击了隐私条款")
                    TheRouter.build("app/CommonWebActivity").withString("url","http://moto-cloud.qiyumotor.com/mobile/agreement?type=1").withString("text",privacyPolicy).navigation()
                }
                override fun updateDrawState(ds: TextPaint) {
                    ds.color = colorMain
                    ds.isUnderlineText = false
                }
            }, privacyPolicyStart, privacyPolicyEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            MessageNoticeDialog(
                title = "温馨提示",
                message = spannableStringBuilder,
                cancelText = "不同意",
                confirmText = "同意",
                alignment = Gravity.START,
                onConfirmClick = {
                    var stare = SPUtils.getInstance().getInt("stare",1)
                    if (stare == 1){
                        TheRouter.build("app/AdActivity").navigation()
                    }else{
                        TheRouter.build("app/MainActivity").navigation()
                    }
//                //同意隐私政策
//                AppMMKV.splashAgreement = true
//                //记录当前版本号
//                AppMMKV.lastVersionCode = getVersionCode(this)
//                mHandler.sendEmptyMessageDelayed(2, SPLASH_MAX_DELAY)
                },
                onCancelClick = { finish() }
            ).show(supportFragmentManager)
        }else{
            TheRouter.build("app/MainActivity").navigation()
        }

    }
}

